﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RailwayReservationWeb
{
    public class AuthService
    {
        private RailwayReservationDataContext db;

        public AuthService()
        {
            db = new RailwayReservationDataContext();
        }

        public bool CheckUserName(string userName)
        {
            var result = db.Accounts.Where(p =>
                p.UserName.Equals(userName));
            return (result.Any());
        }

        public bool CheckLogin(Account authinfo)
        {
            var result = db.Accounts.Where(p =>
                p.UserName.Equals(authinfo.UserName)
                && p.Password.Equals(authinfo.Password)
                && p.Status == 1);
            if (result.Any())
            {
                var authdata = result.First();
                authinfo.AccountId = authdata.AccountId;
                authinfo.UserName = authdata.UserName;
                authinfo.Password = authdata.Password;
                authinfo.Role = authdata.Role;
                authinfo.Email = authdata.Email;
                authinfo.FullName = authdata.FullName;
                authinfo.SSN = authdata.SSN;
                authinfo.DoB = authdata.DoB;
                authinfo.Gender = authdata.Gender;
                authinfo.Address = authdata.Address;
                authinfo.Phone = authdata.Phone;
                authinfo.Status = authdata.Status;
                return true;
            }
            return false;
        }
    }
}
